<template>
 <div>
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>活动管理</el-breadcrumb-item>
    <el-breadcrumb-item>活动列表</el-breadcrumb-item>
  </el-breadcrumb>

  <!--  卡片模块  -->
  <el-card class="box-card">、

    <!-- 搜索功能 -->
    <el-row :gutter="20">
      <el-col :span="12">

        <!-- 这是我的搜索框 -->
        <el-input placeholder="请输入内容" v-model="userInfo.query" clearable @clear="userListData">
          <el-button slot="append" icon="el-icon-search" @click="userListData"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
      </el-col>
    </el-row>

      <!-- 主要内容模块 -->
     <el-table :data="userList" style="width: 100%" stripe border>
      <el-table-column type="index" label="/"></el-table-column>
      <el-table-column prop="username" label="姓名"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="mobile" label="电话"></el-table-column>
      <el-table-column prop="role_name" label="角色"></el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="userstatuschage(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column  label="操作" width="180px">
        <!-- 还是那句话用我的作用域插槽 就是可以获取到我的上面有的数据 -->
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editDialogshow(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete"  size="mini" @click="removeUser(scope.row.id)"></el-button>
          <el-tooltip effect="dark" content="分配角色" placement="top" :enterable='false'>
            <el-button type="warning" icon="el-icon-setting"  size="mini" @click="RolesInfo(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="userInfo.pagenum"
      :page-sizes="[1, 2, 5, 10, 50]"
      :page-size="userInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>

  <!-- 这个时点击添加按钮的时候弹出来的对话框 -->
  <el-dialog
    title="添加用户"
    :visible.sync="dialogVisible"
    width="45%" @close='cleardailog'>
    <!-- 内容主题区 -->
    <el-form ref="addFromRef" label-width="70px" :model="addForm" :rules="addrules">
        <!-- 1.用户名 -->
        <el-form-item label="用户名" prop='username'>
          <el-input prefix-icon="iconfont icon-user"
          v-model="addForm.username"></el-input>
        </el-form-item>
         <el-form-item label="电话" prop='mobile'>
          <el-input prefix-icon="el-icon-phone"
          v-model="addForm.mobile"></el-input>
        </el-form-item>
         <el-form-item label="邮箱" prop='email'>
          <el-input prefix-icon="el-icon-s-goods"
          v-model="addForm.email"></el-input>
        </el-form-item>
         <el-form-item label="密码" prop='password'>
          <el-input prefix-icon="iconfont icon-lock"
          v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>
    <!-- 底部区域 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </span>
  </el-dialog>

  <!-- 当我点击我的编辑按钮的时候会弹出我的编辑对话框 -->
  <el-dialog
    title="编辑基本信息"
    :visible.sync="editDialogVisible"
    width="45%" @close='clearEditdailog'>
    <!-- 内容主题区 -->
    <el-form ref="editFromRef" label-width="70px" :model="editForm" :rules="editrules">
        <!-- 1.用户名 -->
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop='email'>
          <el-input v-model="editForm.email" ></el-input>
        </el-form-item>
        <el-form-item label="手机号"  prop='mobile'>
          <el-input v-model="editForm.mobile" ></el-input>
        </el-form-item>
    </el-form>
    <!-- 底部区域 -->
    <span slot="footer">
      <el-button @click="editDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="editUserInfo">确 定</el-button>
    </span>
  </el-dialog>

  <!-- 当我的点击我的分配角色按钮会弹出来的编辑对话框 -->
  <el-dialog
    title="分配角色"
    :visible.sync="RolesDialogVisible"
    width="45%" @close='closeRolesdlg'>
    <!-- 内容主题区 -->
    <div>
      <p>当前的角色: {{RolesInfos.username}}</p>
      <p>当前的角色的职位: {{RolesInfos.role_name}}</p>
      <p>角色修改为:
        <el-select v-model="selectedRolesId" placeholder="请选择">
          <el-option
            v-for="item in RolesList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id">
          </el-option>
        </el-select>
      </p>
    </div>
    <!-- 底部区域 -->
    <span slot="footer">
      <el-button @click="RolesDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="RolesSum">确 定</el-button>
    </span>
  </el-dialog>
 </div>
</template>

<script>
export default {
  data () {
    return {
      userInfo: {
        // 搜索关键字
        query: '',
        // 页码
        pagenum: 1,
        // 一页有多少个
        pagesize: 2
      },
      total: 0,
      userList: [],
      dialogVisible: false,
      editDialogVisible: false,
      addForm: {
        username: '',
        mobile: '',
        email: '',
        password: ''
      },
      addrules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱ntm', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码名称', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      },
      // 编辑时获取到的信息
      editForm: {

      },
      editrules: {
        email: [
          { required: true, message: '请输入邮箱ntm', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入正确的手机号', trigger: 'blur' },
          { min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur' }
        ]
      },
      // 这个玩意是显示我的分配权限对话框的
      RolesDialogVisible: false,
      // 这个是我的点击分配权限按钮后 把我当前的一个角色的信息保存在这里
      RolesInfos: {},
      // 所有的角色列表
      RolesList: [],
      // 分配权限时选择的角色的ID值
      selectedRolesId: ''
    }
  },
  methods: {
    async userListData () {
      const { data: res } = await this.$http.get('users', { params: this.userInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.userList = res.data.users
      this.total = res.data.total
      // console.log(res)
    },
    // 一页有多少的选项改变
    handleSizeChange (newsize) {
      this.userInfo.pagesize = newsize
      this.userListData()
    },
    // 页码值的改变
    handleCurrentChange (newcurrent) {
      this.userInfo.pagenum = newcurrent
      this.userListData()
    },
    // 用户状态改变时的方法
    async userstatuschage (userInfo) {
      const { data: res } = await this.$http.put(`users/${userInfo.id}/state/${userInfo.mg_state}`)
      // console.log(res)
      if (res.meta.status !== 200) {
        this.res.mg_state = !this.res.mg_state
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
    },
    // 重置一个我的添加表单
    cleardailog () {
      this.$refs.addFromRef.resetFields()
    },
    clearEditdailog () {
      this.$refs.editFromRef.resetFields()
    },
    // 点击确定之后或提交
    add () {
      this.$refs.addFromRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return console.log('valid')
        const { data: res } = await this.$http.post('users', this.addForm)
        // console.log(res)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success('添加用户成功')
        this.dialogVisible = false
      })
    },
    // 控制编辑对话框的出现与隐藏
    async editDialogshow (id) {
      const { data: res } = await this.$http.get('users/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.editForm = res.data
      // console.log(res)
      this.editDialogVisible = true
    },
    // 编辑弹出对话框点击确定之后或提交
    editUserInfo () {
      this.$refs.editFromRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return console.log(valid)
        const { data: res } = await this.$http.put('users/' + this.editForm.id, {
          email: this.editForm.email,
          mobile: this.editForm.mobile
        })
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success('修改用户数据成功')
        this.editDialogVisible = false
        this.userListData()
      })
    },
    // 确认一下我的 删除按钮 用不用执行
    async removeUser (id) {
      const msg = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.$http.delete('users/' + id)
        this.userListData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(msg)
    },
    // 当我点击我的角色的分配角色后做的操作
    async RolesInfo (info) {
      // console.log(info)
      this.RolesInfos = info

      // 这里是获取到我的所有的角色 以便于我select来选中
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      // console.log(res)
      this.RolesList = res.data
      this.RolesDialogVisible = true
    },
    // 我的提交我的分配角色的时候的事件
    async RolesSum () {
      if (!this.selectedRolesId) {
        return this.$message.error('请输入你要修改的角色')
      }
      const { data: res } = await this.$http.put(`users/${this.RolesInfos.id}/role`, { rid: this.selectedRolesId })
      if (res.meta.status !== 200) {
        return this.$message.error('出错l')
      }
      this.userListData()
      this.RolesDialogVisible = false
    },
    // 当我分配角色的对话框关闭时的事件
    closeRolesdlg () {
      this.selectedRolesId = ''
      this.RolesInfos = {}
    }
  },
  created () {
    this.userListData()
  }

}
</script>

<style scoped>
  .el-card {
    margin-top: 30px;
  }
  .el-table {
    font-size: 12px;
    margin-top: 20px;
  }
  .el-pagination {
    margin-top: 15px;
    margin-left: 20px;
  }
  .iconfont {
    margin-left: 15px;
    color: gray!important;
  }
</style>
